const fs = require('fs');
const path = require('path');
const delay = require('delay');
const { Builder, Browser, By, Key, until } = require('selenium-webdriver');

//chrome版本要和chromedriver版本一致
async function mychrome() {
    let driver = await new Builder().forBrowser(Browser.CHROME).build();
    return driver;
}

async function sleep(seconds = 3) {
    console.log('sleep', seconds, 'seconds');
    await delay(seconds * 1000);
}
async function one_page(driver, url) {
    await sleep();
    console.log('fetching:', url);

    await driver.get(url);

    let elements = await driver.findElements(By.css('.co_content8 a.ulink'));
    let all_address = [];
    for (let element of elements) {
        all_address.push(await element.getAttribute('href'));
    }

    if (!all_address.length) {
        return true;
    }

    console.log('get', all_address.length, 'items');

    let lists = [];
    for (let [index, address] of all_address.entries()) {
        await sleep();

        await driver.get(address);
        let elements = await driver.findElements(By.css('div.title_all h1'));
        let download_title = elements.length > 0 ? await elements[0].getText() : '';

        let elements2 = await driver.findElements(By.css('a[href^="magnet:"]'));
        let download_url = elements2.length > 0 ? await elements2[0].getAttribute('href') : '';

        if (download_title && download_url) {
            console.log('get one item:', download_title);
            lists.push(download_title);
            lists.push(download_url);
            lists.push('');
        }
    }
    await fs.promises.appendFile('result.txt', lists.join('\n'), { encoding: 'utf8' });
    return true;
}

async function start() {
    let urls = [
        "https://www.dygod.net/html/gndy/dyzz/index.html",
        "https://www.dygod.net/html/gndy/dyzz/index_2.html",
        "https://www.dygod.net/html/gndy/dyzz/index_3.html",
        "https://www.dygod.net/html/gndy/dyzz/index_4.html",
        "https://www.dygod.net/html/gndy/dyzz/index_5.html",
    ];

    try {
        //clear result.txt
        await fs.promises.writeFile('result.txt', '', { encoding: 'utf8' });
        let driver = await mychrome();

        for (let url of urls) {
            await one_page(driver, url);
        }
        await driver.quit();
        console.log('success');
    } catch (err) {
        console.error(err.toString());
    }

    
    process.exit();
}

start();